package com.travel.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

 public class LoginInterceptor implements HandlerInterceptor {
    // 目标方法执行之前
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

    //     String id=request.getSession().getId();
    //    System.out.println(id);
        System.out.println(request.getRequestURI());
       //登录不拦截
        if(request.getRequestURI().equals("/page/login/login.html")){
            return  true;
        }
       //登录拦截
        if(request.getSession().getAttribute("username"==null)) {
            System.out.println("null");
            response.sendRedirect("/page/login/login.html");
            return false;
        }
            // 已登录，放行
            return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("post");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }

}
